package net.wanglu.www.spdr.fixer;

import java.util.HashMap;

import net.wanglu.www.spdr.entities.Corpus;

public class DateFixer implements Fixer {

	private HashMap<String, String> months = new HashMap<String, String>(64);
	{
		months.put("January","01"); 
		months.put("February","02"); 
		months.put("Marcy","03"); 
		months.put("April","04"); 
		months.put("May","05"); 
		months.put("June","06"); 
		months.put("July","07"); 
		months.put("August","08"); 
		months.put("September","09"); 
		months.put("October","10"); 
		months.put("November","11"); 
		months.put("December","12");
		months.put("Jan","01"); 
		months.put("Feb","02"); 
		months.put("Mar","03"); 
		months.put("Apr","04"); 
		months.put("May","05"); 
		months.put("Jun","06"); 
		months.put("Jul","07"); 
		months.put("Aug","08"); 
		months.put("Sep","09"); 
		months.put("Oct","10"); 
		months.put("Nov","11"); 
		months.put("Dec","12");
	}
	@Override
	public Corpus fix(Corpus corpus) throws Exception {
		// TODO Auto-generated method stub
		String source = corpus.getSource();
		String date = corpus.getDate();
		if ( source.contains("bbc")) {
			date = bbcDate(date);
		} else if ( source.contains("guardian")) {
			date = guardianDate(date);
		} else if ( source.contains("express")) {
			date = expressDate(date);
		}
		
		corpus.setDate(date);
		return corpus;
	}

	private String bbcDate(String date) throws Exception {
		String[] dates = date.split(" ");
		String dd = dates[0];
		String MM = months.get(dates[1]);
		String yyyy = dates[2];
		return yyyy+"-"+MM+"-"+dd;
	}
	
	private String guardianDate(String date) throws Exception {
		int pos = date.indexOf('T');
		return date.substring(0, pos);
	}
	
	private String expressDate(String date) throws Exception {
		String[] dates = date.split(" ");
		String dd = dates[2].split(",")[0].trim();
		String MM = months.get(dates[1]).trim();
		String yyyy = dates[2].split(",")[1].trim();
		return yyyy+"-"+MM+"-"+dd;
	}
}
